<template>
	<view>

		<view class="search-box">
			<!-- 使用自定义的搜索组件 -->
			<my-search @click="gotoSearch()"></my-search>
		</view>

		<!-- 轮播图的区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item, index) in swiperList" :key="index">
				<navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?id=' + item.id">
					<image :src="item.imageSrc"></image>
				</navigator>
			</swiper-item>
		</swiper>

		<!-- 分类导航区域 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item, index) in navList" :key="index" @click="navClickHandler(item)">
				<image :src="item.imageSrc" class="nav-image"></image>
				<view class="nav-name">{{item.name}}</view>
			</view>
		</view>

		<!-- 楼层区域 -->
		<view class="floor-list">
			<!-- 每层的item项 -->
			<view class="floor-item" v-for="(item, index) in floorList" :key="index">
				<!-- 楼层的标题 -->
				<image :src="item.floor_title.image_src" class="floor-title"></image>
				<!-- 楼层图片区域 -->
				<view class="floor-img-box">
					<!-- 左侧大图片盒子 -->
					<navigator class="left-img-box" :url="item.product_list[0].url">
						<image :src="item.product_list[0].image_src"
							:style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
					</navigator>
					<!-- 右侧4个小图盒子 -->
					<view class="right-img-box">
						<navigator class="right-img-item" v-for="(item2,index2) in item.product_list" :key="index2"
							v-if="index2 != 0" :url="item.product_list[0].url">
							<image :src="item2.image_src" :style="{width: item2.image_width + 'rpx'}" mode="widthFix">
							</image>
						</navigator>
					</view>

				</view>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 轮播图列表
				swiperList: [],
				// 分类导航
				navList: [],
				// 楼层
				floorList: []
			};
		},
		onLoad() {
			// 调用轮播图方法
			this.getSwiperList()
			this.getNavList()
			this.getFloorList()
		},
		methods: {
			async getSwiperList() {
				const res = await uni.$http.get('front/getSwiper')
				// 请求失败
				if (res.data.code != 0) return uni.$showMsg()
				this.swiperList = res.data.data
			},
			async getNavList() {
				const res = await uni.$http.get('front/getNavList')
				// 请求失败
				if (res.data.code != 0) return uni.$showMsg()
				this.navList = res.data.data
			},
			async getFloorList() {
				const res = await uni.$http.get('front/getFloorList')
				// 请求失败
				if (res.data.code != 0) return uni.$showMsg()
				var floorData = JSON.parse(res.data.data)
				floorData.forEach(floor => {
					floor.product_list.forEach(pro => {
						pro.url = '/subpkg/goods_list/goods_list?' + pro.navigator_url.split('?')[1]
					})
				})
				this.floorList = floorData
			},
			navClickHandler(item) {
				// 根据名称跳转页面
				if (item.name == '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 330rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 15px 0;

		.nav-image {
			width: 128rpx;
			height: 128rpx;
		}

		.nav-name {
			text-align: center;
		}
	}

	.floor-img-box {
		display: flex;
	}

	.floor-title {
		width: 30%;
		height: 60rpx;
	}

	.right-img-box {
		display: flex;
		flex-wrap: wrap;
	}
	
	.search-box {
		position: sticky;
		top: 0%;
		z-index: 999;
	}
</style>
